package dev.loong.boot3.tasks;

import org.springframework.scheduling.annotation.Async;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Service;

@Service
public class ScheduleService {
    int count1 = 1;
    int count2 = 1;
    int count3 = 1;
    int count4 = 1;
    // 每秒执行1次
    @Scheduled(fixedRate = 1000)
    // 使用异步线程执行
    @Async
    public void job1() {
        System.out.println("【" + Thread.currentThread().getName() + "】" + "【job1】每秒执行一次，执行第【" + count1 + "】次");
        count1 ++;
    }

    @Scheduled(fixedRate = 1000)
    @Async
    public void job2() {
        System.out.println("【" + Thread.currentThread().getName() + "】" + "【job2】每秒执行一次，执行第【" + count2 + "】次");
        count2 ++;
    }

    // 在IOC容器完成初始化后，第一次延迟3秒，之后每秒执行1次
    @Scheduled(initialDelay = 3000, fixedRate = 1000)
    // 使用异步线程执行
    @Async
    public void job3() {
        System.out.println("【" + Thread.currentThread().getName() + "】" + "【job3】每秒执行一次，执行第【" + count3 + "】次");
        count3 ++;
    }
    // 11：00～11：59,每分钟执行一次
    @Scheduled(cron = "0 * 11 * * ?")
    @Async
    public void job4() {
        System.out.println("【" + Thread.currentThread().getName() + "】" + "【job4】每分钟执行一次，执行第【" + count4 + "】次");
        count4 ++;
    }
}
